package view.table;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

import entiey.Table;
import service.TableService;
import service.impl.TableServiceImpl;
import util.CastUtil;
import util.FileUpload;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class V_tableAdd extends JFrame {

	private JPanel contentPane;
	private JTextField txt_deskno;
	private JTextField txt_node;
	private JButton btn_yes;
	private JButton btn_quit;
	private  File selectFile;
	private JLabel lbl_icon;
	private   File fileIcon;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_tableAdd frame = new V_tableAdd();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_tableAdd() {
		setTitle("桌子管理");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 397, 423);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 10, 361, 364);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("新增桌子");
		lblNewLabel.setBounds(94, 24, 94, 15);
		panel.add(lblNewLabel);
		
		JLabel label = new JLabel("桌子编号：");
		label.setBounds(30, 70, 71, 15);
		panel.add(label);
		
		txt_deskno = new JTextField();
		txt_deskno.setBounds(111, 67, 66, 21);
		panel.add(txt_deskno);
		txt_deskno.setColumns(10);
		
		JLabel label_1 = new JLabel("桌子名称：");
		label_1.setBounds(30, 104, 71, 15);
		panel.add(label_1);
		
		txt_node = new JTextField();
		txt_node.setColumns(10);
		txt_node.setBounds(111, 101, 66, 21);
		panel.add(txt_node);
		
		JLabel label_2 = new JLabel("桌子图片：");
		label_2.setBounds(30, 135, 71, 15);
		panel.add(label_2);
		//添加图片
		lbl_icon = new JLabel("");
		lbl_icon.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount()==2) {
					//创建文件选择窗口
					JFileChooser chooser=new JFileChooser();
					chooser.showOpenDialog(V_tableAdd.this);
					chooser.setFileFilter(new FileNameExtensionFilter("JPG & PNG files", "jpg","png"));
					selectFile=chooser.getSelectedFile();
					if (selectFile!=null && selectFile.exists()) {
						ImageIcon icon=new ImageIcon(selectFile.getAbsolutePath());
						icon.setImage(icon.getImage().getScaledInstance(125, 100, 1));
						lbl_icon.setIcon(icon);
					}
				}
				
			}
		});
		lbl_icon.setIcon(new ImageIcon(V_tableAdd.class.getResource("/images/002.jpg")));
		lbl_icon.setBounds(111, 135, 160, 114);
		panel.add(lbl_icon);
		
		btn_yes = new JButton("确 定");
		btn_yes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Table table=new Table();
				table.setDeskNo(CastUtil.castInt(txt_deskno.getText()));
				table.setNode(txt_node.getText());
//				//图片复制
//				String dir=System.getProperty("user.dir");
//				String fileName="/table_image/"+System.currentTimeMillis()+selectFile.getName();
//				FileUpload.copy(selectFile.getAbsolutePath(), dir+fileName);
//				table.setIcon(fileName);
//				
//				 上传图片
				 String dir=System.getProperty("user.dir");
				 String path=dir
						 +"/table_image/"
				 +CastUtil.format(new Date(),CastUtil.YEAR_PATTERN)+
				 selectFile.getName();
				 upload(selectFile, path);
				 table.setIcon(path);
				
				
				TableService ts=new TableServiceImpl();
				if (ts.add(table)) {
					JOptionPane.showMessageDialog(V_tableAdd.this, "桌子添加成功");
					V_tableAdd.this.setVisible(false);
					
				}else {
					JOptionPane.showMessageDialog(V_tableAdd.this, "桌子添加失败");
				}
			}
		});
		btn_yes.setBounds(55, 298, 93, 23);
		panel.add(btn_yes);
		
		btn_quit = new JButton("取 消");
		btn_quit.setBounds(174, 298, 93, 23);
		panel.add(btn_quit);
	}
	/**
	 * 上传头像到项目文件下的menuImg里，数据库记录的是文件的存储路径
	 * @param menuIcon 在电脑本地选择的文件
	 * @param last 保存文件的路径
	 */
	public void upload(File menuIcon,String last) {
		try {
			System.out.println(menuIcon);
			System.out.println(last);
			BufferedInputStream bis=new BufferedInputStream(new FileInputStream(menuIcon));
			BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(new File(last))); 
			byte[] b=new byte[1024];
			int len;
			while((len=bis.read(b))!=-1) {
				bos.write(b,0,len);
			}
			bos.close();
			bis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
